﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MS.InformationManagement.Core.Support;
using Microsoft.SharePoint;
using MS.InformationManagement.DAL.DocumentCenter;
using MS.InformationManagement.Support;
using MS.InformationManagement.Support.Constants;
using MS.InformationManagement.Core.TransactionsResponse;

namespace MS.InformationManagement.BLL.UserProfileManagement
{
    public class UpdateUserProfileCommand : ICommandWithResponse
    {
        #region private methods

        int _profileID; 
        string _profileName; 
        List<Roles> _roleCollection; 
        bool _isSiteContributor;
        List<VincularPerfiles> _perfiles = new List<VincularPerfiles>();

        #endregion

        #region constructors

        public UpdateUserProfileCommand(int profileID, string profileName, List<Roles> roleCollection, bool isSiteContributor)
        {
            _profileID = profileID;
            _profileName = profileName;
            _roleCollection = roleCollection;
            _isSiteContributor = isSiteContributor;
        }

        #endregion

        #region Implemented Methods

        public ITransactionResponse Execute()
        {
            UpdateUserProfileCommandResponse elementToUpgrade = new UpdateUserProfileCommandResponse();
            string oldProfileName = string.Empty;

            using (DCDocumentCenterDataContext ctx = new DCDocumentCenterDataContext(SPContext.Current.Web.Url))
            {
                elementToUpgrade.CurrentPerfil = ctx.Perfiles.SingleOrDefault(profile => profile.Id.Equals(_profileID));
                if (elementToUpgrade.CurrentPerfil != default(VincularPerfiles))
                {
                    _perfiles.Add(elementToUpgrade.CurrentPerfil);

                    oldProfileName = elementToUpgrade.CurrentPerfil.Perfil;

                    if (oldProfileName != _profileName)
                    {
                        var entity = (from item in ctx.Perfiles
                                      where item.Perfil == _profileName
                                      select item).FirstOrDefault();

                        if (entity != null)
                            throw new Exception(string.Format("El perfil con nombre {0} ya existe en la lista de perfiles", _profileName));
                    }

                    elementToUpgrade.CurrentPerfil.Perfil = _profileName;
                    elementToUpgrade.CurrentPerfil.ContribuyenteEnSitio = _isSiteContributor;
                    elementToUpgrade.CurrentPerfil.TipoRole.Clear();
                    _roleCollection.ForEach(delegate(Roles role)
                    {
                        elementToUpgrade.CurrentPerfil.TipoRole.Add(role);
                    });
                    ctx.SubmitChanges();
                }
            }

            if (elementToUpgrade != null) { UpdateProfileProperties(oldProfileName, elementToUpgrade.CurrentPerfil.Perfil); }

            return elementToUpgrade;
    
        }

        public void Undo()
        {
            using (var dc = new DCDocumentCenterDataContext(SPContext.Current.Web.Url))
            {
                foreach (var perfil in _perfiles)
                {
                    var entity = (from item in dc.Perfiles
                                  where item.Id == perfil.Id
                                  select item).FirstOrDefault();

                    if (entity != null)
                    {
                        entity.Perfil = perfil.Perfil;
                        entity.ContribuyenteEnSitio = perfil.ContribuyenteEnSitio;
                        entity.TipoRole = perfil.TipoRole;
 
                    }
                    dc.SubmitChanges();
                }
            }
        }

        #endregion

        #region private methods
        private static void UpdateProfileProperties(string oldProfileName, string newProfileName)
        {
            Dictionary<string, string> users = PeopleHelper.GetUsersByProperty(GlobalConstants.UP_USER_PERFIL, oldProfileName, true);
            foreach (KeyValuePair<string, string> user in users)
            {
                PeopleHelper.UpdatePropertyUserProfile(GlobalConstants.UP_USER_PERFIL, newProfileName, user.Value);
            }
        }

        #endregion         
    }
}
